package com.woniuxy.email.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//生产者
@RestController
@RequestMapping("/producer")
@Slf4j
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //简单队列
    @RequestMapping("/simpleQueue")
    public ResponseEntity simpleQueue(String message) {
        log.info("生产者发送消息：{}", message);
        //将消息发送到队列中
        rabbitTemplate.convertAndSend("simple.queue", message);
        return ResponseEntity.ok("消息发送成功");
    }

    //广播模式
    @RequestMapping("/fanout")
    public ResponseEntity fanout(String message) {
        log.info("生产者广播消息：{}", message);
        //将消息发送到交换机: 交换机 路由键(空) 消息
        rabbitTemplate.convertAndSend("fanout.exchange", null, message);
        return ResponseEntity.ok("消息发送成功");
    }


    //定向模式
    @RequestMapping("/direct")
    public ResponseEntity direct(String message, String key) {
        log.info("生产者定向消息：{}", message);
        //将消息发送到交换机: 交换机 路由键 消息
        rabbitTemplate.convertAndSend("direct.exchange", key, message);
        return ResponseEntity.ok("消息发送成功");
    }

    //主题模式
    @RequestMapping("/topic")
    public ResponseEntity topic(String message, String key) {
        log.info("生产者主题消息：{}", message);
        //将消息发送到交换机: 交换机 路由键 消息
        rabbitTemplate.convertAndSend("topic.exchange", key, message);
        return ResponseEntity.ok("消息发送成功");
    }

}